import api from '../api/api';

const fetchHomeList = response => ({
  type: 'FETCH_HOME_LIST',
  homeList: response
});

export const getHomeList = () => async (dispatch, getState) => {
  try {
    let response = await api.get(`/news/latest`);
    await dispatch(fetchHomeList(response.data));
  } catch (error) {
    console.log('error: ', error);
  }
};

// 侧边栏
const fetchThemes = response => ({
  type: 'FETCH_THEMES',
  themes: response
});

export const getThemes = () => async (dispatch, getState) => {
  try {
    let response = await api.get(`/themes`);
    await dispatch(fetchThemes(response.data));
  } catch (error) {
    console.log('error: ', error);
  }
};
// 点击侧边栏切换栏目
const fetchTheme = response => ({
  type: 'FETCH_THEME',
  theme: response
});

export const getTheme = id => async (dispatch, getState) => {
  try {
    let response = await api.get(`/theme/` + id);
    await dispatch(fetchTheme(response.data));
  } catch (error) {
    console.log('error: ', error);
  }
};
// 获取新闻详情
const fetchNewsDetails = response => ({
  type: 'FETCH_NEWS_DETAILS',
  newsDetails: response
});

export const getNewsDetails = id => async (dispatch, getState) => {
  try {
    let response = await api.get(`/news/` + id);
    await dispatch(fetchNewsDetails(response.data));
  } catch (error) {
    console.log('error: ', error);
  }
};
// 首页上拉加载更多
const fetchHomeNewsMore = response => ({
  type: 'FETCH_HOME_NEWS_MORE',
  homeMoreNews: response
});

export const getHomeNewsMore = id => async (dispatch, getState) => {
  try {
    let response = await api.get(`/news/before/` + id);
    await dispatch(fetchHomeNewsMore(response.data));
  } catch (error) {
    console.log('error: ', error);
  }
};
// 各个新闻上拉加载更多
const fetchNewsMore = response => ({
  type: 'FETCH_NEWS_MORE',
  moreNews: response
});

export const getNewsMore = (id, nID) => async (dispatch, getState) => {
  try {
    if (nID) {
      let response = await api.get(`/theme/${id}/before/${nID}`);
      await dispatch(fetchNewsMore(response.data));
    } else {
      console.log('error: 没有 nID');
    }
  } catch (error) {
    console.log('error: ', error);
  }
};

// story-extra
const fetchStoryExtra = response => ({
  type: 'FETCH_STORY_EXTRA',
  storyExtra: response
});

export const getStoryExtra = id => async (dispatch, getState) => {
  try {
    let response = await api.get(`/story-extra/${id}`);
    await dispatch(fetchStoryExtra(response.data));
  } catch (error) {
    console.log('error: ', error);
  }
};

// long-comments
const fetchLongComments = response => ({
  type: 'FETCH_LONG_COMMENTS',
  longComments: response
});

export const getLongComments = id => async (dispatch, getState) => {
  try {
    let response = await api.get(`/story/${id}/long-comments`);
    await dispatch(fetchLongComments(response.data));
  } catch (error) {
    console.log('error: ', error);
  }
};
// short-comments
const fetchShortComments = response => ({
  type: 'FETCH_SHORT_COMMENTS',
  shortComments: response
});

export const getShortComments = id => async (dispatch, getState) => {
  try {
    let response = await api.get(`/story/${id}/short-comments`);
    await dispatch(fetchShortComments(response.data));
  } catch (error) {
    console.log('error: ', error);
  }
};
// short-comments more
const fetchShortCommentsMore = response => ({
  type: 'FETCH_SHORT_COMMENTS_MORE',
  shortCommentsMore: response
});

export const getShortCommentsMore = (id, sID) => async (dispatch, getState) => {
  try {
    let response = await api.get(`/story/${id}/short-comments/before/${sID}`);
    await dispatch(fetchShortCommentsMore(response.data));
  } catch (error) {
    console.log('error: ', error);
  }
};
